package edu.sysu.cshare.gui;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;

import edu.sysu.cshare.control.MessageManager;
import edu.sysu.cshare.model.Message;
import edu.sysu.cshare.model.Message.Category;
import edu.sysu.cshare.util.gui.GUI;
import edu.sysu.cshare.util.gui.TableModel_Msg;
import edu.sysu.cshare.util.gui.Table_Msg;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Pnl_Message extends JPanel {
	private MessageManager messageManager;
	private JTabbedPane tabpaneContent;

	public static class Image {
		public static ImageIcon getDownload() {
			return new ImageIcon(
					Pnl_Homework.class
							.getResource("/edu/sysu/cshare/image/hm_down.png"));
		}
	}

	/**
	 * Create the panel.
	 */
	public void updateMessage(){
		
		if(tabpaneContent!=null){
			remove(tabpaneContent);
		}
		tabpaneContent = new JTabbedPane(JTabbedPane.TOP);
		add(tabpaneContent, BorderLayout.CENTER);

		for(Category category: Category.values()){
			JPanel pnl = new JPanel(new BorderLayout());
			Table_Msg table = new Table_Msg(new TableModel_Msg(category, messageManager), messageManager);
			
			pnl.add(new JScrollPane(table));
			tabpaneContent.addTab(category.toString(), null, pnl, null);
		}
	}
	public Pnl_Message(MessageManager messageManager) {
		this.messageManager = messageManager;
		setLayout(new BorderLayout(0, 0));
		
		JPanel pnlCtrl = new JPanel();
		add(pnlCtrl, BorderLayout.NORTH);
		pnlCtrl.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
		
		JButton btnNew = new JButton("发布消息");
		btnNew.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				GUI.showModalDialog(new Dlg_Msg(Pnl_Message.this.messageManager, null));
				updateMessage();
			}
		});
		pnlCtrl.add(btnNew);
		updateMessage();
		

	}

}
